<!--
 * @Descripttion: 推广图设置
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 11:33:25
-->
<template>
    <div class="lb-sys-sproconfig">
        <top-nav :isBack="true"></top-nav>
        <div class="page-main">

          <el-row class="page-top-operate">
            <lb-button size='medium' type='primary' @click="$router.push('/app/cash/editImage')" icon='el-icon-plus'>{{$t('menu.CashAddImage')}}</lb-button>
          </el-row>

            <el-table
            v-loading="loading"
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
            <el-table-column
              prop="img"
              label="活动图片">
                <template slot-scope="scope">
                  <lb-image :src="[scope.row.img]" />
                </template>
            </el-table-column>
            <el-table-column
              prop="title"
              label="标题">
            </el-table-column>
            <el-table-column
              prop="type"
              label="类型">
              <template slot-scope="scope">
                <div>{{scope.row.type === 0 ? '封面图' : scope.row.type === 1 ? '详情图' : '分享图'}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="创建时间">
              <template slot-scope="scope">
                <p>{{scope.row.create_time | handleTime(1)}}</p>
                <p>{{scope.row.create_time | handleTime(2)}}</p>
              </template>
            </el-table-column>
            <el-table-column
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <!-- status_text 1未开始，2进行中，3已结束-->
                    <lb-button size='mini' plain type='primary' @click="$router.push('/app/cash/editImage?id=' + scope.row.id)">{{$t('action.edit')}}</lb-button>
                    <lb-button size='mini' plain type='danger' @click="toDel(scope.row.id)">{{$t('action.delete')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>

        <lb-page
          :batch='false'
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
        </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      total: 0,
      searchForm: {
        page: 1,
        limit: 10,
        type: 999
      },
      tableData: []
    }
  },
  activated () {
    this.getTableDataList()
  },
  methods: {
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let {searchForm} = this
      let {code, data} = await this.$api.cashImgList(searchForm)
      // console.log(data, '数据')
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
      this.navTitle = data.active_name ? data.active_name + '的领券记录' : '领券记录'
    },
    async toDel (id) {
      let confirmText = this.$t('tips.confirmDelete')
      this.$confirm(confirmText, this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toConfirmDel(id)
      }).catch(() => {

      })
    },
    async toConfirmDel (id) {
      let {code} = await this.$api.cashImgUpdate({id, status: -1})
      if (code !== 200) return
      this.$message.success(this.$t('tips.successOper'))
      this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
      this.getTableDataList()
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input,
.el-select{
  width: 200px;
}
</style>
